import os
import requests
from lxml import etree
import hashlib
from urllib.parse import urljoin
import os.path
import threading


def download_images(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
        'Referer': url
    }

    try:
        # 发起请求获取页面内容
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        res_html = response.text

        # 使用lxml解析页面内容
        tree = etree.HTML(res_html)

        # 提取所有图片链接
        img_list = tree.xpath('//*[@id="sina_keyword_ad_area2"]/a/img/@real_src')

        # 创建一个锁对象，用于多线程同步
        lock = threading.Lock()

        def download_image(img_url):
            try:
                # 发起请求下载图片
                response = requests.get(img_url, headers=headers)
                response.raise_for_status()
                data = response.content

                # 获取图片后缀
                img_extension = os.path.splitext(img_url)[1]

                # 生成文件名
                filename = hashlib.md5(img_url.encode()).hexdigest() + img_extension

                # 保存图片
                with lock:
                    with open(os.path.join(f'./img/{filename}.jpg'), 'wb') as fp:
                        fp.write(data)

                print("图片下载完成！")
            except Exception as e:
                print("图片下载出错:", str(e))

        # 创建线程列表
        threads = []
        for img in img_list:
            # 处理相对路径的图片链接
            img_url = urljoin(url, img)

            # 创建线程并启动
            thread = threading.Thread(target=download_image, args=(img_url,))
            thread.start()
            threads.append(thread)

        # 等待所有线程完成
        for thread in threads:
            thread.join()

    except Exception as e:
        print("页面请求出错:", str(e))


# 测试代码
url = "https://blog.sina.com.cn/s/blog_01ebcb8a0102ziry.html"
download_images(url)
